package com.zyw.mvc.support;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;

/**
 * @author huoli_zhangyw
 * @date 16/9/7 16:47
 */
public class WrappedResultSerialize extends JsonSerializer<WrappedResult> {
    @Override
    public void serialize(WrappedResult value, JsonGenerator jgen, SerializerProvider serializerProvider)
            throws IOException, JsonProcessingException {
        jgen.writeStartObject();
        jgen.writeNumberField("code", value.getCode());
        jgen.writeStringField("msg", value.getMsg());
        if (value.getErrMsg() != null) {
            jgen.writeStringField("errMsg", value.getErrMsg());
        }
        Object data = value.getData();
        if (data != null) {
            jgen.writeObjectField("data", data);
        } else {
            jgen.writeObjectField("data", "");
        }
        jgen.writeEndObject();
    }
}
